package org.example.day03;

/**
 * 第二章节2.1.6.1:synchronized锁重入(父子继承类中)
 **/
public class MyThread9 {
    public int i = 10;
    synchronized public void operateMainMethod(){
        try {
            i--;
            System.out.println("main print i = " + i);
            Thread.sleep(100);
            System.out.println(this + " ☆ ☆ ☆ ☆ " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Thread9A t9 = new Thread9A();
        t9.start();
    }
}

class Sub extends MyThread9{
    synchronized public void operateSubMethod(){
        try {
            while(i>0){
                i--;
                System.out.println("Sub print i = " + i);
                System.out.println(this + " ★ ★ ★ ★ " + Thread.currentThread().getName());
                this.operateMainMethod();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Thread9A extends Thread{
    @Override
    public void run() {
        Sub sub = new Sub();
        sub.operateSubMethod();
    }
}
